package wnhz.com.business.groupshop.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.activity.shoppingcart.SkuSettingActivity;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 商家中心
 */
public class MerchatCenterActivity extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.tv_allnum)
    TextView tvAllnum;
    @BindView(R.id.tv_new)
    TextView tvNew;
    @BindView(R.id.tv_guanzhu)
    TextView tvGuanzhu;
    @BindView(R.id.ll_quanxian)
    LinearLayout llQuanxian;
    @BindView(R.id.ll_1)
    LinearLayout ll1;

    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchat_center);
        ButterKnife.bind(this);

        actionbar.setData("商家中心", R.drawable.ic_lefft_white, null, R.drawable.ic_shangjia_edit, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, MerchatCenterActivity.class);
        return intent;
    }


    @Override
    public void onLeftClick() {
        finish();

    }

    @Override
    public void onRightClick() {
        seepop();
    }

    @Override
    public void onEditTextClick() {

    }


    @OnClick(R.id.ll_quanxian)
    public void onViewClicked() {
//        startActivity(PayActivity.createIntent(this));
        startActivity(PayActivity.createIntent(MerchatCenterActivity.this, "", "", ""));
    }

    private void seepop() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.item_pop, null);
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int sd = width / 125;
        popupWindow = new PopupWindow(contentView);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
//        popupWindow.showAsDropDown(actionbar,width/(sd-1)*125,0, Gravity.TOP);
        popupWindow.showAsDropDown(actionbar, 0, 0, Gravity.TOP);
        LinearLayout ll_headd = (LinearLayout) contentView.findViewById(R.id.ll_headd);
        LinearLayout ll_headw = (LinearLayout) contentView.findViewById(R.id.ll_headw);
        TextView tv_shangpin = (TextView) contentView.findViewById(R.id.tv_shangpin);
        TextView tv_ordercenter = (TextView) contentView.findViewById(R.id.tv_ordercenter);
        TextView tv_setsku = (TextView) contentView.findViewById(R.id.tv_setsku);
        TextView tv_ruler = (TextView) contentView.findViewById(R.id.tv_ruler);

        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        tv_ruler.setHeight(getViewHeight(ll1, true)+40+ getViewHeight(ll_headw, true)+statusBarHeight1);

        ll_headd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        tv_shangpin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //商品中心
                startActivity(ShangpinActivity.createIntent(MerchatCenterActivity.this));
                popupWindow.dismiss();
            }
        });
        tv_ordercenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //订单中心
                startActivity(OederCenterActivity.createIntent(MerchatCenterActivity.this));
                popupWindow.dismiss();
            }
        });
        tv_setsku.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置sku
//                startActivity(SetSKUActivity.createIntent(MerchatCenterActivity.this));
                startActivity(SkuSettingActivity.createIntent(getApplication()));
                popupWindow.dismiss();
            }
        });
    }

    /**
     * 获取控件的高度或者宽度  isHeight=true则为测量该控件的高度，isHeight=false则为测量该控件的宽度
     *
     * @param view
     * @param isHeight
     * @return
     */
    public static int getViewHeight(View view, boolean isHeight) {
        int result;
        if (view == null) return 0;
        if (isHeight) {
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(h, 0);
            result = view.getMeasuredHeight();
        } else {
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(0, w);
            result = view.getMeasuredWidth();
        }
        return result;
    }
}
